<template>
	<view class="container">
		<view class="carousel">
			<swiper indicator-dots circular=true duration="400">
				<swiper-item class="swiper-item" v-for="(item,index) in goodsData.goods_image" :key="index">
					<view class="image-wrapper">
						<image :src="item" class="loaded" mode="aspectFill"></image>
					</view>
				</swiper-item>
			</swiper>
		</view>

		<view class="introduce-section">
			<text class="title">{{ goodsData.goods_name }}</text>
			<view class="price-box">
				<text class="price-tip">&yen;</text>
				<text class="price">{{ goodsData.price }}</text>
			</view>
			<view class="bot-row">
				<text>销量: 9999+</text>
				<text>库存: {{ goodsData.goods_stock}}</text>
				<text>浏览量: 9999+</text>
			</view>
		</view>

		<view class="c-list">
			<view class="c-row b-b" @click="toggleSpec">
				<text class="tit">购买类型</text>
				<view class="con">
					<text class="selected-text" v-for="(sItem, sIndex) in specSelected" :key="sIndex">
						{{sItem.name}}
					</text>
				</view>
				<text class="yticon icon-you"></text>
			</view>
			<!-- 			<view class="c-row b-b">
				<text class="tit">促销活动</text>
				<view class="con-list">
					<text>新人首单送20元无门槛代金券</text>
					<text>订单满50减10</text>
					<text>订单满100减30</text>
					<text>单笔购买满两件免邮费</text>
				</view>
			</view> -->
			<view class="c-row b-b">
				<text class="tit">服务</text>
				<view class="bz-list con">
					<text>7天无理由退换货 ·</text>
					<text>假一赔十 ·</text>
				</view>
			</view>
			<view class="c-row b-b">
				<text class="tit">是否包邮</text>
				<view class="bz-list con">
					<text>{{ goodsData.is_free_shipping ? '是' : '否'}}</text>
				</view>
			</view>
		</view>

		<view class="detail-desc">
			<view class="d-header">
				<text>图文详情</text>
			</view>
			<u-parse :content="goodsData.goods_content"></u-parse>
			<!-- <rich-text :nodes="goodsData.goods_content"></rich-text> -->
		</view>

		<!-- 底部操作菜单 -->
		<view class="page-bottom">
		<!-- 	<view @click="jumtRouter('/pages/index/index')" class="p-b-btn">
				<text class="yticon icon-xiatubiao--copy"></text>
				<text>首页</text>
			</view> -->
			<view class="p-b-btn">
				<text class="yticon icon-gouwuche"></text>
				<text>分享</text>
				<button class="shareStyle" open-type="share" plain>
					隐藏
				</button>
			</view>
			<view class="action-btn-group">
				<button type="primary" class=" action-btn no-border add-cart-btn" @click="selectGoods('加入购物袋')">
					加入购物袋
				</button>
			</view>
			<view class="action-btn-group" style="margin-right: 10rpx;">
				<button type="primary" class=" action-btn no-border add-cart-btn" @click="selectGoods('立即购买')">
					选择商品
				</button>
			</view>
		</view>

		<product-modal :visible="visibleShow" :product="goodsData" @cancel="visibleShow = false" 
		    :btnTitle="goodsBtnTitle"
			@add-to-cart="handleAddToCartInModal"></product-modal>

	</view>
</template>

<script>
	import request from "@/api/request";
	import productModal from '@/pages/shop/components/product-modal.vue'
	export default {
		components: {
			productModal
		},
		data() {
			return {
				goodsId: null,
				visibleShow: false,
				specSelected: [],
				goodsData: {},
				goodsBtnTitle: '',
				cartData: []
			};
		},
		onLoad({ id }) {
			this.goodsId = id
			this.getGoodsDetail(id)
			this.cartData = uni.getStorageSync('card') || []
		},
		onShareAppMessage(res) {
			if (res.from === 'button') { // 来自页面内分享按钮
				console.log(res.target)
			}
			return {
				title: '热销商品',
				path: `/pages/product/product?id=${this.goodsId}`
			}
		},
		methods: {
			getGoodsDetail(id) {
				uni.showLoading({
				  title: '加载中...'
				})
				request({
					url: '/kx/Shop/detail',
					method: 'post',
					data: {
						goods_id: id
					},
					successCb: (res) => {
						res.data.data.goods_image = res.data.data.goods_image.split(',')
						this.goodsData = res.data.data || []
						this.goodsData.currentNum = 0
						uni.hideLoading()
						// console.log(this.goodsData)
					},
					failCb: (res) => {
					  uni.hideLoading()	
					}
				})
			},
			handleAddToCartInModal(val) {
				if(this.goodsBtnTitle === "加入购物袋"){
					console.log(val,'1')
					let flag = true
					this.cartData.forEach(item => {
					  if(item.sku_id === val.sku_id){
						 flag = false 
					  }
					})
					if(flag){
						val.min_buy ? (val.currentNum = val.min_buy) : (val.currentNum = 1)
					  this.cartData.push(val)
					  this.$store.commit('setCard', this.cartData)
					  uni.$u.toast('已添加')
					   this.visibleShow = false
					} else {
						uni.$u.toast('已存在购物车!')
					}
				}
				if(this.goodsBtnTitle === "立即购买"){
				 val.currentNum ? '' : val.currentNum = val.min_buy
				 if(val.currentNum === 0){
					val.currentNum =  1 
				 }
				 if (val.min_buy !== 0 && val.max_buy !== 0) {
				 	if (val.currentNum < val.min_buy) {
				 		uni.$u.toast(`最少购买${val.min_buy}`)
				 		return
				 	}
				 	if (val.currentNum > val.max_buy) {
				 		uni.$u.toast(`最多购买${val.max_buy}`)
				 		return
				 	}
				 }
				 this.visibleShow = false
				 uni.setStorageSync('buy', [val])
				 uni.navigateTo({
				 	url: '/pages/goodsOrder/index?flag=1'
				 })
				}
			},
			selectGoods(val) {
				request({
					url: '/kx/Shop/getInfoForCategory',
					method: "post",
					data: {
						sku_id: this.goodsData.sku_id
					},
					successCb: (res) => {
						this.goodsData.goods_spec_format = JSON.parse(res.data.data.goods_spec_format)
						this.goodsData.goods_spec_format ? '' : this.goodsData.goods_spec_format = ''
						this.goodsData.sku_image = 'https://kx.taipingkeji.com/' + res.data.data.sku_image
					}
				})
				this.goodsBtnTitle = val
				this.visibleShow = true
			},
			stopPrevent() {},
			jumtRouter(url) {
				uni.navigateTo({
					url
				})
			}
		},

	}
</script>

<style lang='scss'>
	page {
		background: #F4F4F4;
		padding-bottom: 160rpx;
	}

	.icon-you {
		font-size: 28rpx;
		color: #888;
	}

	.carousel {
		height: 722rpx;
		position: relative;

		swiper {
			height: 100%;
		}

		.image-wrapper {
			width: 100%;
			height: 100%;
		}

		.swiper-item {
			display: flex;
			justify-content: center;
			align-content: center;
			height: 750rpx;
			overflow: hidden;

			image {
				width: 100%;
				height: 100%;
			}
		}

	}

	/* 标题简介 */
	.introduce-section {
		background: #fff;
		padding: 20rpx 30rpx;

		.title {
			font-size: 32rpx;
			color: #3d3d3d;
			height: 50rpx;
			line-height: 50rpx;
		}

		.price-box {
			display: flex;
			align-items: baseline;
			height: 64rpx;
			padding: 10rpx 0;
			font-size: 26rpx;
			color: #fa436a;
		}

		.price {
			font-size: 32rpx;
		}

		.m-price {
			margin: 0 12rpx;
		}

		.coupon-tip {
			align-items: center;
			padding: 4rpx 10rpx;
			background: $uni-color-primary;
			font-size: 22rpx;
			color: #fff;
			border-radius: 6rpx;
			line-height: 1;
			transform: translateY(-4rpx);
		}

		.bot-row {
			display: flex;
			align-items: center;
			height: 50rpx;
			font-size: 22rpx;

			text {
				flex: 1;
			}
		}
	}

	/* 分享 */
	.share-section {
		display: flex;
		align-items: center;
		color: 24rpx;
		background: linear-gradient(left, #fdf5f6, #fbebf6);
		padding: 12rpx 30rpx;

		.share-icon {
			display: flex;
			align-items: center;
			width: 70rpx;
			height: 30rpx;
			line-height: 1;
			border: 1px solid $uni-color-primary;
			border-radius: 4rpx;
			position: relative;
			overflow: hidden;
			font-size: 22rpx;
			color: $uni-color-primary;

			&:after {
				content: '';
				width: 50rpx;
				height: 50rpx;
				border-radius: 50%;
				left: -20rpx;
				top: -12rpx;
				position: absolute;
				background: $uni-color-primary;
			}
		}

		.icon-xingxing {
			position: relative;
			z-index: 1;
			font-size: 24rpx;
			margin-left: 2rpx;
			margin-right: 10rpx;
			color: #fff;
			line-height: 1;
		}

		.tit {
			font-size: 24rpx;
			margin-left: 10rpx;
		}

		.icon-bangzhu1 {
			padding: 10rpx;
			font-size: 30rpx;
			line-height: 1;
		}

		.share-btn {
			flex: 1;
			text-align: right;
			font-size: 22rpx;
			color: $uni-color-primary;
		}

		.icon-you {
			font-size: 22rpx;
			margin-left: 4rpx;
			color: $uni-color-primary;
		}
	}

	.c-list {
		font-size: 22rpx + 2rpx;
		color: 24rpx;
		background: #fff;

		.c-row {
			display: flex;
			align-items: center;
			padding: 20rpx 30rpx;
			position: relative;
		}

		.tit {
			width: 140rpx;
		}

		.con {
			flex: 1;
			color: dark;

			.selected-text {
				margin-right: 10rpx;
			}
		}

		.bz-list {
			height: 40rpx;
			font-size: 22rpx+2rpx;
			color: dark;

			text {
				display: inline-block;
				margin-right: 30rpx;
			}
		}

		.con-list {
			flex: 1;
			display: flex;
			flex-direction: column;
			color: dark;
			line-height: 40rpx;
		}

		.red {
			color: $uni-color-primary;
		}
	}

	/* 评价 */
	.eva-section {
		display: flex;
		flex-direction: column;
		padding: 20rpx 30rpx;
		background: #fff;
		margin-top: 16rpx;

		.e-header {
			display: flex;
			align-items: center;
			height: 70rpx;
			font-size: 22rpx + 2rpx;

			.tit {
				font-size: 24rpx + 2rpx;
				color: dark;
				margin-right: 4rpx;
			}

			.tip {
				flex: 1;
				text-align: right;
			}

			.icon-you {
				margin-left: 10rpx;
			}
		}
	}

	.eva-box {
		display: flex;
		padding: 20rpx 0;

		.portrait {
			flex-shrink: 0;
			width: 80rpx;
			height: 80rpx;
			border-radius: 100px;
		}

		.right {
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: 24rpx;
			color: 24rpx;
			padding-left: 26rpx;

			.con {
				font-size: 24rpx;
				color: dark;
				padding: 20rpx 0;
			}

			.bot {
				display: flex;
				justify-content: space-between;
				font-size: 22rpx;
			}
		}
	}

	/*  详情 */
	.detail-desc {
		background: #fff;
		margin-top: 16rpx;

		.d-header {
			display: flex;
			justify-content: center;
			align-items: center;
			height: 80rpx;
			font-size: 24rpx + 2rpx;
			color: dark;
			position: relative;

			text {
				padding: 0 20rpx;
				background: #fff;
				position: relative;
				z-index: 1;
			}

			&:after {
				position: absolute;
				left: 50%;
				top: 50%;
				transform: translateX(-50%);
				width: 300rpx;
				height: 0;
				content: '';
				border-bottom: 1px solid #ccc;
			}
		}
	}

	/* 规格选择弹窗 */
	.attr-content {
		padding: 10rpx 30rpx;

		.a-t {
			display: flex;

			image {
				width: 170rpx;
				height: 170rpx;
				flex-shrink: 0;
				margin-top: -40rpx;
				border-radius: 8rpx;
				;
			}

			.right {
				display: flex;
				flex-direction: column;
				padding-left: 24rpx;
				font-size: 22rpx + 2rpx;
				color: 24rpx;
				line-height: 42rpx;

				.price {
					font-size: 32rpx;
					color: $uni-color-primary;
					margin-bottom: 10rpx;
				}

				.selected-text {
					margin-right: 10rpx;
				}
			}
		}

		.attr-list {
			display: flex;
			flex-direction: column;
			font-size: 24rpx + 2rpx;
			color: 24rpx;
			padding-top: 30rpx;
			padding-left: 10rpx;
		}

		.item-list {
			padding: 20rpx 0 0;
			display: flex;
			flex-wrap: wrap;

			text {
				display: flex;
				align-items: center;
				justify-content: center;
				background: #eee;
				margin-right: 20rpx;
				margin-bottom: 20rpx;
				border-radius: 100rpx;
				min-width: 60rpx;
				height: 60rpx;
				padding: 0 20rpx;
				font-size: 24rpx;
				color: dark;
			}

			.selected {
				background: #fbebee;
				color: $uni-color-primary;
			}
		}
	}

	/*  弹出层 */
	.popup {
		position: fixed;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		z-index: 99;

		&.show {
			display: block;

			.mask {
				animation: showPopup 0.2s linear both;
			}

			.layer {
				animation: showLayer 0.2s linear both;
			}
		}

		&.hide {
			.mask {
				animation: hidePopup 0.2s linear both;
			}

			.layer {
				animation: hideLayer 0.2s linear both;
			}
		}

		&.none {
			display: none;
		}

		.mask {
			position: fixed;
			top: 0;
			width: 100%;
			height: 100%;
			z-index: 1;
			background-color: rgba(0, 0, 0, 0.4);
		}

		.layer {
			position: fixed;
			z-index: 99;
			bottom: 0;
			width: 100%;
			min-height: 40vh;
			border-radius: 10rpx 10rpx 0 0;
			background-color: #fff;

			.btn {
				height: 66rpx;
				line-height: 66rpx;
				border-radius: 100rpx;
				background: $uni-color-primary;
				font-size: 24rpx + 2rpx;
				color: #fff;
				margin: 30rpx auto 20rpx;
			}
		}

		@keyframes showPopup {
			0% {
				opacity: 0;
			}

			100% {
				opacity: 1;
			}
		}

		@keyframes hidePopup {
			0% {
				opacity: 1;
			}

			100% {
				opacity: 0;
			}
		}

		@keyframes showLayer {
			0% {
				transform: translateY(120%);
			}

			100% {
				transform: translateY(0%);
			}
		}

		@keyframes hideLayer {
			0% {
				transform: translateY(0);
			}

			100% {
				transform: translateY(120%);
			}
		}
	}

	/* 底部操作菜单 */
	.page-bottom {
		position: fixed;
		left: 30rpx;
		bottom: 30rpx;
		z-index: 95;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 690rpx;
		height: 100rpx;
		background: rgba(255, 255, 255, .9);
		box-shadow: 0 0 20rpx 0 rgba(0, 0, 0, .5);
		border-radius: 16rpx;

		.shareStyle {
			position: absolute;
			opacity: 0;
			border: none !important;
			font-size: 24rpx;
		}

		.p-b-btn {
			position: relative;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			font-size: 22rpx;
			color: 24rpx;
			width: 96rpx;
			height: 80rpx;

			.yticon {
				font-size: 40rpx;
				line-height: 48rpx;
			}

			&.active,
			&.active .yticon {
				color: $uni-color-primary;
			}

			.icon-fenxiang2 {
				font-size: 42rpx;
				transform: translateY(-2rpx);
			}

			.icon-shoucang {
				font-size: 46rpx;
			}
		}

		.action-btn-group {
			display: flex;
			height: 76rpx;
			border-radius: 100px;
			overflow: hidden;
			box-shadow: 0 20rpx 40rpx -16rpx #FF8732;
			background: linear-gradient(90deg, #FF8732 0%, #FFB65E 100%);
			margin-left: 20rpx;
			position: relative;

			.action-btn {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 260rpx;
				height: 100%;
				font-size: 24rpx;
				padding: 0;
				border-radius: 0;
				background: transparent;
			}
		}
	}
</style>